// pages/enterprise/user/personnel-list/index.js
import {getEnterpriseList} from "../../../service/Personnel";
import Config from "../../../main.config";
import {getStorageSync} from "../../../utils/Storage";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        _personnel: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.data._companyId = getStorageSync(Config.storage.registerInfo + ".id");
        this.setData({"_personnel.loading": true});
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let paging = e.detail.paging;
            let companyId = this.data._companyId;
            let list = this.data._personnel.list;
            let isReset = this.data._personnel.reset;
            let res = await getEnterpriseList({
                status: 2,
                page: paging.page,
                limit: paging.pageSize,
                companyId
            });
            let newList = res.data.map(function (item) {
                let logoUrl = Config.api.downLoad + "?fileId=" + item.logo
                return {
                    id: item.id,
                    companyId: item.companyId,
                    logo: logoUrl,
                    name: item.name,
                    deptName: item.deptName,
                    position: item.position,
                    phone: item.phone,
                    auth: item.auth
                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "_personnel.list": list,
                "_personnel.total": res.count
            });
        } catch (e) {
            this.setData({
                "_personnel.error": true
            })
        } finally {
            this.setData({
                "_personnel.reset": false,
                "_personnel.loading": false
            })
        }
    },

    /*
    * 点击事件监听
    * 点击栏目 跳转编辑页面
    * */
    bindNavigatorToEdit: function (e) {
        const that = this;
        let list = this.data._personnel.list;
        let index = e.currentTarget.dataset.index
        wx.navigateTo({
            url: "/packageEnterprise/pages/personnel-edit/index?id=" + list[index].id
            + "&companyId=" + list[index].companyId
            + "&auth=" + list[index].auth,
            events: {
                acceptDataFromOpenedPage: function(data) {
                    that.setData({"_personnel.reset": true})
                }
            },
        })
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"_personnel.loading": true})
    }
})